<template>
  <el-container class="index-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/Sielogo (2).png" alt="" />
        <span class="info">用户权限管理系统</span>
      </div>
      <!-- <el-button type="info" @click="logout()">退出</el-button> -->
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          {{ this.user.name }}<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <!-- <el-dropdown-item command="a">查看用户角色</el-dropdown-item> -->
          <!-- <el-dropdown-item command="c">查看用户权限</el-dropdown-item> -->
          <el-dropdown-item command="b">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-header>
    <!-- 主体区 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333741"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i class="el-icon-setting"></i>
              <!-- 文本 -->
              <span>{{ item.menuName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.menuAddstress"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState()"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.menuName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-container>
        <!-- 主体区 -->
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
    <div>
      <el-dialog title="用户权限" :visible.sync="getPower" width="60%">
        <span>这是一段信息</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="getPower = false">取 消</el-button>
          <el-button type="primary" @click="getPower = false">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </el-container>
</template>

<script>
import Menu from "./menu/Menu.vue";
export default {
  components: { Menu },
  data() {
    return {
      menuList: [],
      isCollapse: false,
      activePath: "",
      user: {},
      getRoles: false,
      getPower: false,
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
    this.user = JSON.parse(window.sessionStorage.getItem("user"));
    console.log(this.user);
  },
  methods: {
    handleCommand(command) {
      switch (command) {
        case "a":
          this.getRolesF();
          break;
        case "b":
          this.logout();
          break;
        case "c":
          this.getPowerF();
          break;
      }
    },
    logout: function () {
      this.$axios.get("/api/logout");
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    getMenuList: async function () {
      const { data: res } = await this.$axios.get("/api/menu");
      console.log(res);
      this.menuList = res.data;
      // console.log(this.menuList);
    },
    //点击按钮切换菜单折叠与展开
    toggleCollapse: function () {
      this.isCollapse = !this.isCollapse;
    },
    saveNavState: function (activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
    // getRolesF:async function () {
    //   const { data: res } = await this.$axios.post(
    //       "/api/user/queryRolerById",
    //       JSON.parse(window.sessionStorage.getItem("user")).id
    //     );
    //   console.log(res);
    //   this.getRoles = true;
    // },
    getPowerF: function () {
      this.getPower = true;
    },
  },
};
</script>

<style scoped>
.index-container {
  height: 100vh;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.el-header div {
  display: flex;
  align-items: center;
}
.el-header div img {
  margin-left: 20px;
}
.el-header div .info {
  margin-left: 15px;
  font-size: 25px;
  letter-spacing: 3px;
}
.el-header .el-dropdown-link {
  cursor: pointer;
  color: #eff;
  margin-right: 15px;
}
.el-header .el-dropdown-link:hover {
  color: #409eff;
}

.el-header .el-dropdown-link .el-icon-arrow-down {
  font-size: 12px;
}
.el-aside {
  background-color: #333744;
  transition: 0.5s;
}
.el-aside .el-menu {
  border-right: none;
}
.el-main {
  background-color: #F5FAF6;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
}
</style>